export function urlEncode(options){
    let params = ''
    if (options){
        params = Object.keys(options).map(key => {
            return encodeURIComponent(key) + '=' + encodeURIComponent(options[key])
        }).join('&')
    }

    return params
}


export function showToast(title, options={}){
    let timeOut = 1500
    wx.showToast({
        title: title,
        icon: options.icon ? options.icon : 'none',
        duration: timeOut,
        success: function (res) {
            if(options.success){
                options.success(res)
            }
            // 消失时执行
            setTimeout(() => {
                if (options.timeout){
                    options.timeout(res)
                }
            }, timeOut)
        }
    })

}

export const showToastError = (title, options={}) => {
    options.icon = 'error'
    showToast(title, options)
}

export const showToastSuccess = (title, options={}) => {
    options.icon = 'success'
    showToast(title, options)
}

export const showModel = (content, options={}) => {

    const _options = {
        ...{
            title: '提示',
        },
        ...options
    }

    wx.showModal({
        title: _options.title,
        content: content,
        success (res) {
            if (res.confirm && _options.confirm) {
                _options.confirm()
            } else if (res.cancel && _options.cancel) {
                _options.cancel()
            }
        }
    })

}



/**
 * 转换rpx为px
 *
 * @description
 * 什么时候用？
 * - 布局(width: 172rpx)已经写好, 某些组件只接受px作为style或者prop指定
 *
 */
export const rpx2px = (rpx, round = false) => {
    let { screenWidth, pixelRatio } = wx.getSystemInfoSync()

    // px / systemWidth = rpx / 750
    const result = (rpx * screenWidth) / 750;

    if (round) {
        return Math.floor(result);
    }

    return result||0;
};

export const px2rpx = (px, round = false) => {
    let { screenWidth, pixelRatio } = wx.getSystemInfoSync()

    // px / systemWidth = rpx / 750
    const  result = px * 750 / screenWidth

    if (round) {
        return Math.floor(result);
    }

    return result||0;
};




export const getLoginCode = () => {

    return new Promise((resolve, reject) => {
        wx.login({
            success (res) {

                console.log("loginCode", res)

                if (res.code) {
                    resolve(res.code)
                } else {
                    showToast("登录code失败", res.errMsg)
                    reject(res.errMsg)
                }
            }
        })

    })


}


/*函数节流*/
export const throttle = (fn, interval) => {
    var enterTime = 0; //触发的时间
    var gapTime = interval || 300; //间隔时间，如果interval不传值，默认为300ms
    return function() {
        var that = this;
        var backTime = new Date(); //第一次函数return即触发的时间
        if(backTime - enterTime > gapTime) {
            fn.call(that, arguments[0]);
            enterTime = backTime; //赋值给第一次触发的时间 保存第二次触发时间
        }
    };
}

/*函数防抖*/
export const debounce = (fn, interval) => {
    var timer;
    var gapTime = interval || 1000; //间隔时间 不传值默认为1000ms
    return function() {
        clearTimeout(timer);
        var that = this;
        var args = arguments; //保存arguments setTimeout是全局的 arguments不是防抖函数需要的
        timer = setTimeout(function() {
            fn.call(that, args[0]);
        }, gapTime);
    };
}
